package expection01;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class OneDay02 {
    public static void main(String[] args) throws Exception {
        throwDemo();
        tryCatchDemo();
        tryCatchFinallyDemo();
        autoCloseableDemo();
        pwDemo();
        brDemo();
    }

    private static void brDemo()throws Exception {
        System.out.println("brDemo()测试开始");
        BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream("pw1.txt"),StandardCharsets.UTF_8));
        String line ;
        System.out.println("println-------br1");
        while ((line = br1.readLine()) != null){
            System.out.println(line);
        }
        System.out.println("print-------br1");
        while ((line = br1.readLine()) != null){
            System.out.print(line);
        }
        br1.close();
        System.out.println();
        System.out.println("------------------------------------");
        System.out.println();
        FileInputStream fis = new FileInputStream("pw2.txt");
        InputStreamReader isr = new InputStreamReader(fis,StandardCharsets.UTF_8);
        BufferedReader br2 = new BufferedReader(isr);
        System.out.println("println-------br2");
        while ((line = br2.readLine()) != null){
            System.out.println(line);
        }
        System.out.println("print-------br2");
        while ((line = br2.readLine()) != null){
            System.out.print(line);
        }
        br2.close();
        System.out.println("brDemo()测试结束");
    }

    private static void pwDemo() throws Exception {
        System.out.println("pwDemo---pw1测试开始");
        PrintWriter pw1 = new PrintWriter("pw1.txt");
        pw1.println("pw1---println写入一");
        pw1.println("pw1---println写入二");
        pw1.print("pw1---print写入一");
        pw1.print("pw1---print写入二");
        pw1.close();
        System.out.println("pwDemo---pw1测试结束");
        System.out.println();
        System.out.println("------------------------------------");
        System.out.println();
        System.out.println("pwDemo---pw2测试开始");
        FileOutputStream fos = new FileOutputStream("pw2.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw2 = new PrintWriter(bw);
        while (true){
            Scanner scan = new Scanner(System.in);
            String line = scan.nextLine();
            if("exit".equals(line)){
                break;
            }
            pw2.println(line);
        }
        pw2.println("pw2---println写入一");
        pw2.println("pw2---println写入二");
        pw2.print("pw2---print写入一");
        pw2.print("pw2---print写入二");
        pw2.close();
        System.out.println("pwDemo---pw2测试结束");

    }

    private static void autoCloseableDemo() {
        System.out.println("autoCloseableDemo()开始");
        try(FileOutputStream fos = new FileOutputStream("ziji.txt")){
            fos.write(333);
        }catch (Exception e){
            System.out.println("文件异常已经被捕获并处理");
        }
        System.out.println("autoCloseableDemo()结束");
    }

    private static void tryCatchFinallyDemo() {
        System.out.println("tryCatchFinallyDemo()开始");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("jdk.txt");
            fos.write(11);
        }catch (Exception e) {
            System.out.println("文件操作异常已经处理");
        }finally {
            try {
                fos.close();
            }catch (Exception e){
                System.out.println("文件关闭时的异常已经处理");
            }
        }
        System.out.println("tryCatchFinallyDemo()结束");
    }

    private static void tryCatchDemo() {
        System.out.println("tryCatchDemo方法开始");
        try {
            String line = null;
            System.out.println(line.charAt(0));
        } catch (StringIndexOutOfBoundsException e) {
            System.out.println("字符串下标越界异常已经处理");
        }catch (Exception e){
            System.out.println("通用异常已经处理");
        }
        System.out.println("tryCatchDemo方法结束");
    }

    private static void throwDemo() {
        Student mz = new Student();
        try {
            mz.setAge(97);
        } catch (Exception e) {
            System.out.println("年龄输入有误");
        }
        System.out.println("此人的年龄是："+mz.getAge());

        Student kz = new Student();
        try {
            kz.setAge(-100);
        } catch (Exception e) {
            System.out.println("年龄输入有误");
        }
        System.out.println("此人的年龄是："+kz.getAge());
    }
}
